/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.main.header;

import javax.swing.JLabel;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jfonia.connect5.basics.Observer;
import org.jfonia.constants.SymbolConstants;
import org.jfonia.constants.ViewConstants;
import org.jfonia.language.LabelConstants;
import org.jfonia.language.LanguageResource;
import org.jfonia.notation.Notation;

/**
 *
 * @author Rik Bauwens
 */
public class RestPanel extends NotationPanel
{
        public RestPanel()
        {
                super(LanguageResource.getInstance().getLabel(LabelConstants.RESTS));
        }

        @Override
        protected void init()
        {
                    JLabel restLabel = new JLabel(LanguageResource.getInstance().
                            getLabel(LabelConstants.REST));
                    addLabel(restLabel);
                    GlyphLabel rest1 = new GlyphLabel(this, SymbolConstants.REST_1,
                                                          NotationType.REST, ViewConstants.REST1);
                    addOption(rest1);
                    GlyphLabel rest1_2 = new GlyphLabel(this, SymbolConstants.REST_1_2,
                                                          NotationType.REST, ViewConstants.REST1_2);
                    addOption(rest1_2);
                    GlyphLabel rest1_4 = new GlyphLabel(this, SymbolConstants.REST_1_4,
                                                          NotationType.REST, ViewConstants.REST1_4);
                    addOption(rest1_4);
                    GlyphLabel rest1_8 = new GlyphLabel(this, SymbolConstants.REST_1_8,
                                                          NotationType.REST, ViewConstants.REST1_8);
                    addOption(rest1_8);
                    GlyphLabel rest1_16 = new GlyphLabel(this, SymbolConstants.REST_1_16,
                                                          NotationType.REST, ViewConstants.REST1_16);
                    addOption(rest1_16);
                    GlyphLabel rest1_32 = new GlyphLabel(this, SymbolConstants.REST_1_32,
                                                          NotationType.REST, ViewConstants.REST1_32);
                    addOption(rest1_32);
                    addSpacing();

                    JLabel dotsLabel = new JLabel(LanguageResource.getInstance().
                            getLabel(LabelConstants.DOTS));
                    addLabel(dotsLabel);
                    final SpinnerModel dotsModel = new SpinnerNumberModel(Notation.getInstance().getRestDots(), 0,
                                                                       ViewConstants.MAX_DOTS, 1);
                    final CustomSpinner dots = new CustomSpinner(dotsModel);
                    Notation.getInstance().getRestDotsNode().addObserver(new Observer(){
                            public void onNotify(Object source)
                            {
                                    if(((Integer) dotsModel.getValue()).intValue() != Notation.getInstance().getRestDots())
                                            dotsModel.setValue(Notation.getInstance().getRestDots());
                            }
                    });
                    dotsModel.addChangeListener(new ChangeListener(){
                            public void stateChanged(ChangeEvent e)
                            {
                                    if(Notation.getInstance().getRestDots() != ((Integer) dotsModel.getValue()).intValue())
                                            Notation.getInstance().setRestDots((Integer) dotsModel.getValue());
                            }
                    });
                    dots.setMaximumSize(dots.getPreferredSize());
                    addOption(dots);
        }

}
